import tkinter as tk
import redis
import os
from util import get_delete_data_from_redis
import tkinter.font as tkFont
import json
from PIL import Image, ImageTk


top_color = "#2c5cb3"
mid_color = "#3399FF"



root = tk.Tk()
root.attributes('-fullscreen', True)

font_style_content = tkFont.Font(family="AR PL UKai CN", size=14)
font_style_top = tkFont.Font(family="AR PL UMing CN", size=28)
font_style_bottom = tkFont.Font(family="AR PL UMing CN", size=18)
font_style_bottom_total = tkFont.Font(family="AR PL UMing CN", size=20, weight="bold")




# 设置网格的行和列权重
root.grid_rowconfigure(0, weight=2)
root.grid_rowconfigure(1, weight=6)
root.grid_rowconfigure(2, weight=2)
root.grid_columnconfigure(0, weight=1)

# 创建三个Frame分别用于上中下部分
frame_top = tk.Frame(root, bg=top_color)
frame_top.grid(row=0, column=0, sticky="nsew")
frame_top.pack_propagate(False)  # 禁止frame_top自动调整大小

frame_center = tk.Frame(root, bg=mid_color)
frame_center.grid(row=1, column=0, sticky="nsew")
frame_top.pack_propagate(False)  # 禁止frame_top自动调整大小


frame_bottom = tk.Frame(root, bg=top_color)
frame_bottom.grid(row=2, column=0, sticky="nsew")
frame_bottom.pack_propagate(False)  # 禁止frame_top自动调整大小


# 顶部文字
label_product_top = tk.Label(frame_top, text="选购商品", bg=top_color, fg="white", font=font_style_top)
label_product_top.pack(expand=True, fill="both")

frame_top.grid_rowconfigure(0, weight=1)
frame_top.grid_columnconfigure(0, weight=1)



# 底部状态
label_status = tk.Label(frame_bottom, text="设备状态：门锁打开", bg=top_color, fg="white", font=font_style_bottom)
label_status.pack(side="left", expand=True, fill="x", padx=50)

# 底部总计
label_total = tk.Label(frame_bottom, text="共 10 件商品，￥10.00元", bg=top_color, fg="white", font=font_style_bottom_total)
label_total.pack(side="left", expand=True, fill="x", padx=50)

frame_bottom.grid_columnconfigure(0, weight=1)
frame_bottom.grid_columnconfigure(1, weight=1)


# 中间商品渲染

goods_list = [
    {'image_path': 'maofeng_1704177712061.jpg', 'tea_name': "蒙山毛峰", "number": "2"},        
    {'image_path': 'maofeng_1704177712061.jpg', 'tea_name': "蒙山毛毛", "number": "3"},
    {'image_path': 'maofeng_1704177712061.jpg', 'tea_name': "蒙山毛毛", "number": "3"},
    {'image_path': 'maofeng_1704177712061.jpg', 'tea_name': "蒙山毛毛", "number": "3"},
    {'image_path': 'maofeng_1704177712061.jpg', 'tea_name': "蒙山毛毛", "number": "3"},
    {'image_path': 'maofeng_1704177712061.jpg', 'tea_name': "蒙山毛毛", "number": "3"},
    {'image_path': 'maofeng_1704177712061.jpg', 'tea_name': "蒙山毛毛", "number": "3"},
    {'image_path': 'maofeng_1704177712061.jpg', 'tea_name': "蒙山毛毛", "number": "3"},

]


def display_images(image_list):

    desired_width, desired_height = 80,80
    num_per_row = 5  # 每行展示的图片数量

    for i, image_info in enumerate(image_list):
        row = i // num_per_row  # 计算所在行数
        column = i % num_per_row  # 计算所在列数

        frame_goods = tk.Frame(frame_center, bg=mid_color)
        frame_goods.grid(row=row, column=column, padx=10, pady=10)  # 设置间距

        # 加载图片
        image_path = image_info['image_path']
        image = Image.open(image_path)
        image = image.resize((desired_width, desired_height))

        photo = ImageTk.PhotoImage(image)

        label_image = tk.Label(frame_goods, image=photo, bg=mid_color)
        label_image.pack(side="top")

        label_text1 = tk.Label(frame_goods, text=image_info['tea_name'], bg=mid_color, fg="black", font=font_style_content)
        label_text1.pack(side="top")

        label_text2 = tk.Label(frame_goods, text="X " + image_info['number'], bg=mid_color, fg="black", font=font_style_content)
        label_text2.pack(side="top")

        label_image.image = photo  # 需要保持对图片对象的引用，以防止被垃圾回收

frame_center.grid_columnconfigure(0, weight=1)
frame_center.grid_columnconfigure(1, weight=1)
frame_center.grid_columnconfigure(2, weight=1)
frame_center.grid_columnconfigure(3, weight=1)
frame_center.grid_columnconfigure(4, weight=1)
frame_center.grid_rowconfigure(0, weight=1)
frame_center.grid_rowconfigure(1, weight=1)


display_images(goods_list)





root.mainloop()